<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\service\BaseService;
use app\exam\logic\admin\ModuleExamination as ModuleExaminationLogic;
use app\exam\logic\admin\Paper as PaperLogic;
use app\common\logic\category\admin\Category;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use Exception;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;

/**
 * Class ModuleExamination
 *
 * @package app\exam\service\admin
 */
class ModuleExamination
{
    /**
     * @Inject()
     *
     * @var ModuleExaminationLogic
     */
    protected $moduleExaminationLogic;

    /**
     * @Inject()
     *
     * @var Category
     */
    protected $category;

    /**
     * @Inject()
     *
     * @var PaperLogic
     */
    protected $paperLogic;

    /**
     * 试卷配置
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/4
     *
     * @param int $moduleId
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function config(int $moduleId)
    {
        $map      = ['module_id' => $moduleId];
        $topicMap = [];
        // 关键字
        Request::has('title', 'get', true) && $topicMap[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // 试卷类型
        if (Request::has('paper_type', 'get', true) && Request::get('paper_type/d', -1) !== -1) {
            $topicMap[] = ['paper_type', '=', Request::get('paper_type/d')];
        }
        // 分类
        $category = Request::get('category/s', '');
        $category && $topicMap[] = ['category', 'like', '%,' . $category . ','];

        $data = $this->moduleExaminationLogic->getList($map, $topicMap);

        // 定义字段
        $keyList   = [
            'title'           => ListItem::make()->title('试卷名称'),
            'paper_type_text' => ListItem::make()->title('试卷类型'),
            'cate_title'      => ListItem::make()->title('试卷分类'),
            'topic_count'     => ListItem::make()->title('题数'),
            'price'           => ListItem::make('custom')->title('价格'),
            'status'          => ListItem::make('custom')->title('状态'),
            // 'credit_redeem'   => ListItem::make('custom')->title('积分抵扣购买'),
            'update_time'     => ListItem::make()->title('更新时间'),
        ];
        $category  = $this->category->cascader(0, 3);
        $paperType = $this->paperLogic->getPaperTypeOptions();
        // 搜索字段
        $searchKey = [
            'paper_type' => FormItem::make('select')->title(false)->placeholder('试卷类型')->options($paperType),
            'category'   => FormItem::make('cascader')->title(false)->placeholder('试卷分类')->options($category),
            'title'      => FormItem::make()->title(false)->placeholder('试卷名称')->maxlength(30),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->datas     = $data;
        $list->searchKey = $searchKey;

        return $list->send();
    }

    /**
     * 获取选项面板配置数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @return Response
     */
    public function getChooseOptions()
    {
        $category  = $this->category->cascader(0, 3);
        $paperType = $this->paperLogic->getPaperTypeOptions();

        return BaseService::parseToData([
            'options' => [
                'category'   => $category,
                'paper_type' => $paperType,
            ]
        ], 1);
    }

    /**
     * 获取试卷列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/4
     *
     * @param int $moduleId
     * @return Response
     * @throws DbException
     */
    public function getChooseList(int $moduleId)
    {
        $map = [];
        // 关键字
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // 试卷类型
        if (Request::has('paper_type', 'get', true) && Request::get('paper_type/d', -1) !== -1) {
            $map[] = ['paper_type', '=', Request::get('paper_type/d')];
        }
        // 分类
        $category = Request::get('category/s', '');
        $category && $map[] = ['category', '=', $category];

        // 难易度
        $difficulty = Request::get('difficulty/d', 0);
        $difficulty && $map[] = ['point', '=', $difficulty];
        $data = $this->moduleExaminationLogic->getChooseList($moduleId, $map);
        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->datas = $data;

        return $list->send();
    }

    /**
     * 新建试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/31
     *
     * @param int $moduleId
     * @return Response
     * @throws Exception
     */
    public function create(int $moduleId)
    {
        $data = Request::post('papers/a');
        $this->moduleExaminationLogic->createModuleExaminationPaper($moduleId, $data);

        return BaseService::parseToData([], 1, '保存成功');
    }

    /**
     * 编辑试卷某个字段
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/31
     *
     * @param int $examinationId
     * @return Response
     */
    public function edit(int $examinationId)
    {
        $value = Request::put();

        $this->moduleExaminationLogic->editModuleExaminationPaper($examinationId, $value);

        return BaseService::parseToData([], 1, '编辑成功');
    }

    /**
     * 编辑试卷某个字段
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/31
     *
     * @param int    $examinationId
     * @param string $field
     * @return Response
     */
    public function editField(int $examinationId, string $field)
    {
        $value = Request::put($field);

        $this->moduleExaminationLogic->editModuleExaminationPaperField($examinationId, $field, $value);

        return BaseService::parseToData([], 1, '编辑成功');
    }

    /**
     * 移除试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @param string $id
     * @return Response
     */
    public function delete(string $id)
    {
        $this->moduleExaminationLogic->deleteModuleExaminationPaper($id);

        return BaseService::parseToData([], 1, '移除成功');
    }

    /**
     * 批量移除试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @param string $ids
     * @return Response
     */
    public function deletes(string $ids)
    {
        $this->moduleExaminationLogic->deleteModuleExaminationPaper($ids);

        return BaseService::parseToData([], 1, '批量移除成功');
    }
}
